/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import persistence.entity.Customer;

/**
 *
 * @author Craig
 */
@Stateless
public class CustomerFacade extends AbstractFacade<Customer> implements CustomerFacadeRemote {
    @PersistenceContext(unitName = "Entity-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomerFacade() {
        super(Customer.class);
    }

    
    @Override
    public boolean authenticate(int custID, String custPass, boolean isATM) {
        
        Customer cust = find(custID);
        if (cust == null) {
            System.out.println("cust is null");
            return false;
        }
        
        boolean passMatch = cust.getCustPassword().equals(custPass);
        if (!passMatch) {
            System.out.println("pass is not match");
            return false;
        }
        
        boolean disabled = Boolean.parseBoolean(cust.getIsdisabled());
        if (disabled) {
            return false;
        }
        
        boolean isTeller = Boolean.parseBoolean(cust.getCustIsTeller());
        System.out.println(cust.getCustId() + ", " + cust.getCustName() + ", " + cust.getCustPassword() + ", " + cust.getCustPhone() + ", " + cust.getCustAddress() + ", " + cust.getCustIsTeller() + ", " + cust.getIsdisabled());
        
        return isATM || isTeller;
    }

    @Override
    public Customer createCustomer(String name, String address, String phone, String password) {
        Customer c = new Customer();
        c.setCustName(name);
        c.setCustAddress(address);
        c.setCustPhone(phone);
        c.setCustPassword(password);
        c.setCustIsTeller(Boolean.FALSE.toString().toLowerCase());
        c.setIsdisabled(Boolean.FALSE.toString().toLowerCase());
        
        try {
            create(c);
            List<Customer> all = findAll();
            Customer lastAdded = all.get(all.size() - 1);
            return lastAdded;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public Boolean updateID(int currentID, int newID) {
       
        Customer c = find(currentID);
        c.setCustId(newID);
        
        try {
            edit(c);
            return true;
        } catch( Exception e ) {
            System.out.println(e.getMessage());
        }
        
        return false;
    }

    @Override
    public Boolean updateName(int id, String name) {
        
        Customer c = find(id);
        c.setCustName(name);

        try {
            edit(c);
            return true;
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
        
        return false;
    }

    @Override
    public Boolean updateAddress(int id, String address) {
        Customer c = find(id);
        c.setCustAddress(address);

        try {
            edit(c);
            return true;
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
        
        return false;
    }

    @Override
    public Boolean updatePhone(int id, String phone) {
        Customer c = find(id);
        c.setCustPhone(phone);

        try {
            edit(c);
            return true;
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
        
        return false;
    }

    @Override
    public Boolean updatePassword(int id, String password) {
     
        Customer c = find(id);
        c.setCustPassword(password);

        try {
            edit(c);
            return true;
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
        
        return false;
    }

    @Override
    public Boolean removeCustomer(int id) {
        Customer c = find(id);
        try {
            remove(c);
            return true;
        } catch ( Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
    
    
    
    
    
    
    
    
    
}
